package com.goods.controller.business;

import com.goods.business.service.InStockService;
import com.goods.common.model.business.InStock;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.InStockVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

@RestController
@RequestMapping("/business/inStock")
public class InStockController {

    @Autowired
    private InStockService inStockService;


    /**
     *  入库单（入库记录）数据分页列表展示
     pageNum=1     起始页
     pageSize=10   每页条数
     status=1      状态
     inNum=123     单号
     type=1        类型
     startTime=2022-04-15+00:00:00   开始时间
     endTime=2022-05-18+00:00:00     结束时间
     */
    @GetMapping("findInStockList")
    public ResponseBean findInStockList(@RequestParam Map map){
        try {

            PageVO<InStockVO> pageVO = inStockService.findInStockList(map);
            return ResponseBean.success(pageVO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("查询失败");
        }
    }

    /**
     * 明细显示
     * @param inStockId
     * @return
     */
    @GetMapping("detail/{inStockId}")
    public ResponseBean detail(@PathVariable Long inStockId,
                               @RequestParam("pageNum")int pageNum){
        try {
            Map<String,Object> map = inStockService.detail(inStockId,pageNum);
            return ResponseBean.success(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("明细显示错误");
        }
    }

    /**
     * 放入回收站
     * @param id
     * @return
     */
    @PutMapping("/remove/{id}")
    public ResponseBean remove(@PathVariable Long id){
        try {
            inStockService.remove(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("放入回收站失败");
        }
    }

    /**
     * 还原回收站
     * @param id
     * @return
     */
    @PutMapping("/back/{id}")
    public ResponseBean back(@PathVariable Long id){
        try {
            inStockService.back(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("放入回收站失败");
        }
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PutMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id){
        try {
            inStockService.delete(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("放入回收站失败");
        }
    }

    /**
     * add入库
     */
    @PostMapping("/addIntoStock")
    public ResponseBean addIntoStock(@RequestBody InStockVO inStockVO,HttpServletRequest request ){
        try {

            inStockService.addIntoStock(inStockVO);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("新增失败");
        }
    }

    /**
     * 审核通过
     */
    @PutMapping("/publish/{id}")
    public ResponseBean publish(@PathVariable Long id){
        try {
            inStockService.publish(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseBean.error("审核失败");
        }
    }
}
